package org.cloud.im.core.ws;

import org.cloud.im.core.ImChannelContext;
import org.cloud.im.core.ImPacket;
import org.cloud.im.core.ImSessionContext;
import org.cloud.im.core.packets.Command;
import org.cloud.im.core.protocol.IProtocolConverter;

/**
 * @Author: w.weidong.lin@abite.com
 * @Description: TODO
 * @Date: create in 2021/10/19 14:39
 */
public class WsConvertPacket implements IProtocolConverter {

    /**
     * WebSocket响应包;
     */
    @Override
    public ImPacket RespPacket(byte[] body, Command command, ImChannelContext channelContext) {
        ImSessionContext sessionContext = channelContext.getSessionContext();
        //转ws协议响应包;
        if (sessionContext instanceof WsSessionContext) {
            WsResponsePacket wsResponsePacket = new WsResponsePacket();
            wsResponsePacket.setBody(body);
            wsResponsePacket.setWsOpcode(Opcode.TEXT);
            wsResponsePacket.setCommand(command);
            return wsResponsePacket;
        }
        return null;
    }

    @Override
    public ImPacket RespPacket(ImPacket imPacket, Command command, ImChannelContext imChannelContext) {

        return this.RespPacket(imPacket.getBody(), command, imChannelContext);
    }

    @Override
    public ImPacket ReqPacket(byte[] body, Command command, ImChannelContext channelContext) {

        return null;
    }
}
